﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ParallelRuntimeLibrary.Collections;
using ParallelRuntimeLibrary;
using System.Diagnostics;

namespace TestProject
{
    public static class SegmentQueueTest
    {
        public static void Test()
        {
            pp:
            ConcurrentQueue<string> o = new ConcurrentQueue<string>();
            Queue<string> p = new Queue<string>();

            Parallel.For(0, 1092, (i, pls) =>
                {

                    lock (p)
                    {
                        p.Enqueue(i.ToString());
                    }
                    o.Enqueue(i.ToString());
                    for (int ii = 0; ii < 32; ii++)
                    {
                        o.Enqueue(i.ToString());
                        lock (p)
                        {
                            p.Enqueue(i.ToString());
                            
                        }
                    }

                    for (int ii = 0; ii < 14; ii++)
                    {
                        string k;
                        bool tmp = o.Dequeue(out k);
                        
                        lock (p)
                            p.Dequeue();
                    }

                });


            Console.WriteLine(p.Count);
            Console.WriteLine(o.Count);
            Console.WriteLine(p.Count == o.Count);

            var oa = o.ToList();

            while(p.Count > 0 ) 
            {
                var tmp = p.Dequeue();

                if (!oa.Contains(tmp))
                    Console.WriteLine("Bad");
            }

           
            Console.ReadLine();
            goto pp;
        }

        public static void Test2()
        {
            //string[] k = new string[] { "alpha", "beta", "gamma", "haytham", "oooo", "pppp" };
            start:
            ConcurrentQueue<string> l = new ConcurrentQueue<string>();

            Stopwatch sw = new Stopwatch();
            sw.Start();

            Parallel.For(0, 520312, (i, pls) =>
                {

                    l.Enqueue(i.ToString());
                    for (int ii = 0; ii < 10; ii++)
                        l.Enqueue(i.ToString());

                    for (int ii = 0; ii < 6; ii++)
                    {
                        string po;
                        l.Dequeue(out po);
                    }

                });


            l.ConcurrentMode = false;
            for (int ii = 0; ii < 1112996; ii++)
                l.Enqueue(ii.ToString());

            for (int ii = 0; ii < 159821; ii++)
            {
                string po;
                l.Dequeue(out po);
            }


            sw.Stop();
            Console.WriteLine("Count : " + l.Count);
            Console.WriteLine("Time : " + sw.ElapsedMilliseconds);
            Console.ReadLine();
            goto start;

        }

        public static void PerformanceTest()
        {
        }
    }
}
